package xyz.whiteblue.controller;

import xyz.whiteblue.core.ActionType;
import xyz.whiteblue.core.CoreFilter;
import xyz.whiteblue.core.UrlAction;
import xyz.whiteblue.kit.StrKit;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by WhiteBlue on 15/6/15.
 */
public class ActionFactory {
    private ActionFactory() {
    }


    public static UrlAction translateUrl(String requestUrl, String method) {
        requestUrl = StrKit.clearUrl(requestUrl);

        ActionType actionType;

        switch (method.toLowerCase()) {
            case "get":
                actionType = ActionType.GET;
                break;
            case "post":
                actionType = ActionType.POST;
                break;
            case "head":
                actionType = ActionType.HEAD;
                break;
            case "put":
                actionType = ActionType.PUT;
                break;
            case "delete":
                actionType = ActionType.DELETE;
                break;
            case "options":
                actionType = ActionType.OPTIONS;
                break;
            default:
                actionType = ActionType.UNKNOWN;
        }


        String controller_key = null;
        String method_key = null;
        Map<Integer, String> urlParams = new HashMap<Integer, String>();

        String[] split0 = requestUrl.split(CoreFilter.slash);
        if (split0.length == 1) {
            controller_key = split0[0];
        } else {
            controller_key = split0[0];
            method_key = split0[1];
            for (int i = 2; i < split0.length; i++) {
                urlParams.put(i - 2, split0[i]);
            }
        }

        return new UrlAction(StrKit.clearUrl(controller_key), StrKit.clearUrl(method_key), actionType, urlParams);


    }
}
